class IUP_MENU_ITEM
-- Creates an item of the menu interface element. When selected, it generates 
-- an action.
-- Menu items are activated using the Enter key.
--
-- In Motif and GTK, the text font will be affected by the dialog font when the 
-- menu is mapped.
--
-- Since GTK 2.14 to have a menu item that can be marked you must set the VALUE 
-- attribute to ON or OFF, or set HIDEMARK=False, before mapping the control.

inherit
	IUP_MENU_ELEMENT
		redefine
			execute_map,
			execute_unmap,
			execute_destroy,
			execute_help,
			execute_action,
			execute_highlight
		end
	IUP_WIDGET_TITLE
	IUP_WIDGET_ACTIVE
	IUP_WIDGET_NAME

create {ANY}
	item

feature {ANY}

	item (title: STRING)
		-- A new item.
		-- title: Text to be shown on the item. It can be Void. The "&" character 
		-- can be used to define a mnemonic, the next character will be used as 
		-- key. Use "&&" to show the "&" character instead on defining a 
		-- mnemonic. When in a menu bar an item that has a mnemonic can be 
		-- activated from any control in the dialog using the "Alt+key" 
		-- combination.
		--
		-- The text also accepts the control character '%T' to force text 
		-- alignment to the right after this character. This is used to add 
		-- shortcut keys to the menu, aligned to the right, ex: "Save%TCtrl+S", 
		-- but notice that the shortcut key (also known as Accelerator or Hot 
		-- Key) still has to be implemented. To implement a shortcut use the K_* 
		-- callbacks in the dialog.
		local
			a_item, a, p: POINTER
		do
			if attached title as text then
				a := get_pointer(text.to_c)
			end
						
			a_item := int_item (a, p)
			set_widget(a_item)
		end

	-- Attibutes

	set_auto_toggle (state: BOOLEAN)
		-- (non inheritable): enables the automatic toggle of VALUE state when 
		-- the item is activated. Default: False.
		do
			iup_open.set_attribute(Current, "AUTOTOGGLE", boolean_to_yesno(state))
		end

	set_hide_mark (state: BOOLEAN)
		-- [Motif and GTK Only]: If enabled the item cannot be checked, since the 
		-- check box will not be shown. If all items in a menu enable it, then no 
		-- empty space will be shown in front of the items. Normally the unmarked 
		-- check box will not be shown, but since GTK 2.14 the unmarked check box 
		-- is always shown. If your item will not be marked you must set 
		-- HIDEMARK=True, since this is the most common case we changed the 
		-- default value to True for this version of GTK, but if VALUE is defined 
		-- the default goes back to False. Default: False.
		do
			iup_open.set_attribute(Current, "HIDEMARK", boolean_to_yesno(state))
		end

	set_image (name: STRING)
		-- [Windows and GTK Only] (non inheritable): Image name of the check mark 
		-- image when VALUE=OFF. In Windows, an item in a menu bar cannot have a 
		-- check mark. Ignored if item in a menu bar. A recommended size would be 
		-- 16x16 to fit the image in the menu item. In Windows, if larger than 
		-- the check mark area it will be cropped.
		do
			iup_open.set_attribute(Current, "IMAGE", name)
		end

	set_image_press (name: STRING)
		-- [Windows and GTK Only] (non inheritable): Image name of the check mark 
		-- image when VALUE=ON.
		do
			iup_open.set_attribute(Current, "IMPRESS", name)
		end

	set_title_image (name: STRING)
		-- (non inheritable): Image name of the title image. In Windows, it 
		-- appears before of the title text and after the check mark area (so 
		-- both title and title image can be visible). In Motif, it must be at 
		-- least defined during map, it replaces the text, and only images will 
		-- be possible to set (TITLE will be hidden). In GTK, it will appear on 
		-- the check mark area. 
		do
			iup_open.set_attribute(Current, "TITLEIMAGE", name)
		end

	set_value (value: BOOLEAN)
		-- (non inheritable): Indicates the item's state. When the value is ON, a 
		-- mark will be displayed to the left of the item. Default: OFF. An item 
		-- in a menu bar cannot have a check mark. When IMAGE is used, the 
		-- checkmark is not shown. See the item AUTOTOGGLE attribute and the menu 
		-- RADIO attribute.
		do
			if value then	
				iup_open.set_attribute(Current, "VALUE", "ON")
			else
				iup_open.set_attribute(Current, "VALUE", "OFF")
			end
		end

	get_value: BOOLEAN
		-- The value.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "VALUE")

			if str.is_equal("ON") then
				Result := True
			else
				Result := False
			end
		end

	-- Callbacks

	-- Common
	set_cb_map (act: detachable FUNCTION[TUPLE[IUP_MENU_ITEM], STRING])
		-- Called right after an element is mapped and its attributes updated.
		local
			operation: INTEGER
		do
			cb_map := act
			
			if cb_map /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
		end

	set_cb_unmap (act: detachable FUNCTION[TUPLE[IUP_MENU_ITEM], STRING])
		-- Called right before an element is unmapped.
		local
			operation: INTEGER
		do
			cb_unmap := act

			if cb_unmap /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
		end

	set_cb_destroy (act: detachable FUNCTION[TUPLE[IUP_MENU_ITEM], STRING])
		-- Called right before an element is destroyed.
		local
			operation: INTEGER
		do
			cb_destroy := act

			if cb_destroy /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
		end

	set_cb_help (act: detachable PROCEDURE[TUPLE[IUP_MENU_ITEM]])
		-- Action generated when the user press F1 at a control. In Motif 
		-- is also activated by the Help button in some workstations 
		-- keyboard.
		-- Returns: IUP_CLOSE will be processed.
		local
			operation: INTEGER
		do
			cb_help := act

			if cb_help /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "HELP_CB", "NONEEDED", operation)
		end

	set_cb_action (act: detachable FUNCTION[TUPLE[IUP_MENU_ITEM], STRING])
		-- Action generated when the item is selected. IUP_CLOSE will be 
		-- processed. Even if inside a popup menu when IUP_CLOSE is returned, the 
		-- current popup dialog or the main loop will be closed.
		local
			operation: INTEGER
		do
			cb_action := act

			if cb_action /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "ACTION", "Fn", operation)
		end

	-- Extra
	set_cb_highlight (act: detachable FUNCTION[TUPLE[IUP_MENU_ITEM], STRING])
		-- Action generated when the item is highlighted.
		local
			operation: INTEGER
		do
			cb_highlight := act

			if cb_highlight /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "HIGHLIGHT_CB", "NONEEDED", operation)
		end

feature {IUP}

	-- Common callbacks
	
	execute_map: STRING
		do
			if attached cb_map as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

	execute_unmap: STRING
		do
			if attached cb_unmap as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

	execute_destroy: STRING
		do
			if attached cb_destroy as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

	execute_help
		do
			if attached cb_help as int_cb then
				int_cb.call([Current])
			end
		end

	execute_action: STRING
		do
			if attached cb_action as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

	-- Extra

	execute_highlight: STRING
		do
			if attached cb_highlight as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

feature {NONE}

	-- For callbacks
	
	cb_map: detachable FUNCTION[TUPLE[IUP_MENU_ITEM], STRING]
	cb_unmap: detachable FUNCTION[TUPLE[IUP_MENU_ITEM], STRING]
	cb_destroy: detachable FUNCTION[TUPLE[IUP_MENU_ITEM], STRING]
	cb_help: detachable PROCEDURE[TUPLE[IUP_MENU_ITEM]]
	cb_action: detachable FUNCTION[TUPLE[IUP_MENU_ITEM], STRING]

	cb_highlight: detachable FUNCTION[TUPLE[IUP_MENU_ITEM], STRING]

	-- Internals
	
	int_item(title, action: POINTER): POINTER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return IupItem ($title, $action);"
      end

end

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017, 2018, 2019, 2020 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
